
<template>
    <div id="loginId">
        <header>
            <i class="logo"></i>
            <span class="title">智能数据交换云服务平台</span>
        </header>
        <section>
            <div class="loggin">
                <el-form  :model="formData">
                <i class="user"></i><input type="text" v-model="formData.username" :autofocus="true" required placeholder="用户名"  >
                <i class="password"></i><input type="password" name="password" required placeholder="密 码" v-model="formData.password" @keyup.enter.native="doSubmit" >
                </el-form>
                <el-button type="primary" @click="doSubmit">登录</el-button>
            </div>
        </section>
    </div>
</template>


<script>
    // import './login2.css';
    import {zuulHost,uamServer,clientID,authHost,tokenUri,getToken,initDictionaryList,webHost} from '../../api/api';
    import utils from '../../api/utils.js';
    // import '../../api/css/loginbase.css';
    setTimeout(function(){
        location.href= webHost+'#/loginSecond';
    },10);
    
    export default {
        data() {
            return {
                formData: {
                    username: 'admin'
                    ,password: 'admin'
                    ,grant_type: 'password'
                }
            };
        },
        methods: {
            doSubmit:function () {
                var _this=this;
                getToken("username="+this.formData.username+"&password="+this.formData.password+"&grant_type="+this.formData.grant_type)
                    .then(res=>{
                        console.log(res);
                        localStorage.token = res.data.access_token;
                        localStorage.retoken = res.data.refresh_token;
                        // 初始化api管理的权限信息  用ajax同步请求 每次登入获取权限信息
                        _this.$jq.ajax({
                            type : "POST", //提交方式
                            url : zuulHost+"/uaa/roleApiPermission/searchByRole?access_token="+localStorage.token,//路径
                            data : "{}",
                            contentType: "application/json", // 请求数据格式(json)
                            async: false, // 同步
                            datatype: "json", // 返回数据格式(json)
                            headers: {"Content-Type":"application/json"},
                            success : function(result) {//返回数据根据结果进行相应的处理
                                if ( result.code === "S100101" ) {
                                    let json = JSON.stringify(result.data);
                                    localStorage.apiPermission = json;
                                    console.log(json);
                                } else {
                                    console.log("api权限获取失败！");
                                }
                            }
                        });
                        // 初始化字典
                        if(!sessionStorage.allDictData||sessionStorage.allDictData=='[]'||utils.isEmpty(JSON.parse(sessionStorage.allDictData))){
                            let params2 = {
                                data:{isPageAble: false},
                                access_token:"?access_token="+localStorage.token
                            }
                            initDictionaryList(params2).then(res=>{
                                sessionStorage.allDictData = JSON.stringify(res.data.data.list) ;
                            }).catch(err=>{
                                console.log(err);
                            });
                        }
                        _this.$router.go(-1);
                        // 这种设置 就算延迟执行，也不确定在延迟时间内，上面的异步请求是否已经完成，所以上面最好用同步请求获取数据
                        // setTimeout(function(){
                        //     _this.$router.go(-1);  // 登入时返回最后一次的路由  延迟执行
                        // },1000);
                    })
                    .catch(err=>{
                        console.log(err);
                    });
            }

        },
        mounted () {
            console.log(zuulHost+uamServer+clientID+authHost+tokenUri);
        }
    }
</script>

